package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bsh.mybatis.model.AdminRole;
import com.bsh.mybatis.model.Role;
import com.bsh.mybatis.model.RoleModule;
import com.bsh.repository.DaoAdminRole;
import com.bsh.repository.DaoRole;
import com.bsh.repository.DaoRoleModule;
import com.bsh.service.RoleService;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 后台理员
 * @author gsh
 *
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
	@Autowired
	private DaoRole roleDao;
	@Autowired
	private DaoRoleModule roleModDao;
	@Autowired
	private DaoAdminRole adminRoleDao;

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#getPage(com.bsh.utils.Pager, java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager,String roleName,String email) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isNullOrEmpty(roleName))
			sb.append(String.format(" and u.name = '%s'",roleName.trim()));
		if(!StringUtils.isNullOrEmpty(email))
			sb.append(String.format(" and u.email = '%s'",email.trim()));		
		List<Map<String, Object>> list = roleDao.getPage(pager,sb.toString());
		return list;
	}
	
	
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#getRole(java.lang.Integer)
	 */
	@Override
	public Role getRole(Integer roleid) {
		return roleDao.selectByPrimaryKey(roleid);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#addRole(com.bsh.mybatis.model.Role)
	 */
	@Override
	public Integer addRole(Role role) {		
		return roleDao.addRole(role);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#editRole(com.bsh.mybatis.model.Role)
	 */
	@Override
	public Integer editRole(Role role) {
		return roleDao.editRole(role);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#enableRole(int, boolean)
	 */
	@Override
	public Integer enableRole(int id, boolean isEnabele) {
		return roleDao.enableRole(id, isEnabele ? 1 : 0);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#disableBatch(int[])
	 */
	@Override
	public Integer disableBatch(int[] idList) {
		if (idList != null && idList.length == 0)
			return 0;
		for (int id : idList) {
			roleDao.enableRole(id, 0);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#enableBatch(int[])
	 */
	@Override
	public Integer enableBatch(int[] idList) {
		if (idList != null && idList.length == 0)
			return 0;
		for (int id : idList) {
			roleDao.enableRole(id, 1);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#deleteRole(int)
	 */
	@Override
	public Integer deleteRole(int roleid) {
		adminRoleDao.removeByRole(roleid);
		roleModDao.removeByRole(roleid);
		int res = roleDao.deleteByPrimaryKey(roleid);

		return res;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#getRoles()
	 */
	@Override
	public List<Role> getRoles() {
		return roleDao.getRoles();
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#setRoleModules(int, java.lang.String[])
	 */
	@Override
	public void setRoleModules(int roleId,String[] moduleList){
		roleModDao.removeByRole(roleId);
		for (String moduleId : moduleList) {
			int moudleId = Integer.parseInt(moduleId);
			RoleModule rmd =new RoleModule();
			rmd.setRoleId(roleId);
			rmd.setMoudleId(moudleId);
			roleModDao.insert(rmd);
		}
		
	}
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#getRolePermission(int)
	 */
	@Override
	public List<RoleModule> getRolePermission(int roleId){
		return roleModDao.getRolePermission(roleId);
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#setAdminRole(int, java.lang.String[])
	 */
	@Override
	public void setAdminRole(int adminId,String[] roleList){
		adminRoleDao.removeByAdmin(adminId);
		for (String roleId : roleList) {
			int rid = Integer.parseInt(roleId);
			AdminRole ar =new AdminRole();
			ar.setRoleId(rid);
			ar.setAdminId(adminId);
			adminRoleDao.insert(ar);
		}		
	}	
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.RoleService#getAdminRoles(int)
	 */
	@Override
	public List<Role> getAdminRoles(int adminId){
		return roleDao.getAdminRoles(adminId);
	}
}
